let result = new Object();

/**
 * 时间戳转换为YY-MM-DD hh:mm:ss格式
 */
result.toCommonFormat = function (time) {
    //将时间戳格式转换成年月日时分秒
    var date = new Date(time);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';

    var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
    var strDate = Y + M + D + h + m + s;
    return strDate;
};

result.format = function (time, formatStr) {
    var date = new Date(time);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());

    var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours());
    var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
    var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());

    var str = formatStr
        .replace(/[yY]+/, Y)
        .replace(/M+/, M)
        .replace(/[Dd]+/, D)
        .replace(/[h]+/, h)
        .replace(/[m]+/, m)
        .replace(/[s]+/, s)
    ;
    return str;
}

/**
 * 获取当前时间的文字描述
 * @param {*} dateTime 时间戳
 */
result.readableDate=function(dateTime) {

    // 获取当前时间戳
    let now = new Date().getTime();
    // 如果小于1分钟，显示刚刚
    if (now - dateTime < 60 * 1000) {
        return "刚刚";
    }

    // 如果大于1分钟，小于1小时，显示几分钟前
    if (now - dateTime < 60 * 60 * 1000) {
        // 获取相差的分钟数
        let minutes = Math.floor((now - dateTime) / (60 * 1000));

        return minutes+"分钟前";
    }
    // 获取今天的年月日和dateTime的年月日
    let nowDate = new Date(now);
    let dateTimeDate = new Date(dateTime);
    // 如果是同一天，显示hh:mm
    if (
        nowDate.getFullYear() == dateTimeDate.getFullYear() &&
        nowDate.getMonth() == dateTimeDate.getMonth() &&
        nowDate.getDate() == dateTimeDate.getDate()
    ) {
        return result.format(dateTime, "hh:mm");
    }
    // 如果是昨天，显示昨天 hh:mm
    if (
        nowDate.getFullYear() == dateTimeDate.getFullYear() &&
        nowDate.getMonth() == dateTimeDate.getMonth() &&
        nowDate.getDate() - dateTimeDate.getDate() == 1
    ) {
        return "昨天 " + result.format(dateTime, "hh:mm");
    }
    // 如果是今年，显示MM-dd hh:mm
    if (
        nowDate.getFullYear() == dateTimeDate.getFullYear() &&
        nowDate.getMonth() == dateTimeDate.getMonth()
    ) {
        return result.format(dateTime, "MM-dd hh:mm");
    }
    // 如果是去年，显示yyyy-MM-dd hh:mm
    return result.format(dateTime, "yyyy-MM-dd hh:mm");
}


export default result;
